package de.grosjohann.android.feuerwehr;

import java.util.List;

import org.json.JSONException;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import de.grosjohann.android.feuerwehr.adapter.CalendarListAdapter;
import de.grosjohann.android.feuerwehr.domain.CalendarItem;
import de.grosjohann.android.feuerwehr.notification.Notify;
import de.grosjohann.android.feuerwehr.panel.Panel;
import de.grosjohann.android.feuerwehr.service.CalendarDisplayService;
import de.grosjohann.android.feuerwehr.service.CalendarNotificationService;
import de.grosjohann.android.feuerwehr.service.IntentServiceFacade;
import de.grosjohann.android.feuerwehr.util.JSON2ObjectUtil;

public class CalendarActivity
  extends Activity
  implements IUpdateUIListener {

  private static final String NAME = "CalendarActivity";

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.calendar);
    ((TextView) findViewById(R.id.tvHeader)).setText(R.string.lblCalendar);

    ListView lv = (ListView) findViewById(R.id.lvCalendarItems);
    BaseAdapter baseAdapter = new CalendarListAdapter(this, R.layout.calendar_item);
    lv.setAdapter(baseAdapter);

    lv.setTextFilterEnabled(false);
    lv.setDividerHeight(10);

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
      Log.i(NAME, "started - has extras");
      updateUI((String) extras.get(CalendarNotificationService.CALENDAR_DATA));
    }

    CalendarDisplayService.updateUIListener = this;
  }

  @Override
  protected void onResume() {
    super.onResume();
    Notify.getInstance().clear(this);

    IntentServiceFacade.startOnce(this, CalendarDisplayService.class);
  }

  @Override
  public void updateUI(final String newData) {
    runOnUiThread(new Runnable() {
      @Override
      public void run() {
        TextView tvNoCalendar = (TextView) findViewById(R.id.tvNoCalendar);

        try {

          /*
           * Liste der Calendar-Items
           */
          List<CalendarItem> list = JSON2ObjectUtil.getCalendarItems(newData);

          ListView lv = (ListView) findViewById(R.id.lvCalendarItems);
          ((CalendarListAdapter) lv.getAdapter()).addAll(list);

          tvNoCalendar.setVisibility(View.GONE);

        } catch (JSONException ex) {
          Log.e(NAME, ex.getMessage(), ex);

          tvNoCalendar.setVisibility(View.VISIBLE);

          Panel panel = new Panel(getApplicationContext(), CalendarActivity.this);
          panel.showError(R.string.errReceiveCalendarData);
        }
      }
    });
  }

  @Override
  public void showInfo(String info) {
    // nothing to do
  }

  @Override
  public void showWarning(String warning) {
    // nothing to do
  }

  @Override
  public void showError(final String error) {
    runOnUiThread(new Runnable() {
      @Override
      public void run() {
        Panel panel = new Panel(getApplicationContext(), CalendarActivity.this);
        panel.showError(error);
      }
    });
  }

}
